import vue from 'vue'
import Vuex from 'vuex'
import getters from './getters'
import { moduleLoader } from '@/shared/index'

vue.use(Vuex)

const modules = moduleLoader(require.context('./modules', true, /\.js$/))

export default new Vuex.Store({
  state: {
    theme: uni.getStorageSync('theme') || 'light',
  },
  modules,
  getters,
  mutations: {
    /**
     * update theme
     * @param {object} state 
     * @param {'light'|'dark'} theme 主题
     */
    SET_THEME(state, theme) {
      state.theme = theme
      uni.setStorageSync('theme', theme)
    }
  },
  actions: {
    /**
     * 切换主题
     * @param {object} params
     * @param {'light'|'dark'} theme 主题
     */
    changeTheme({ commit }, theme) {
      commit('SET_THEME', theme)
    }
  }
})

